package main

import (
	"flag"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/config"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/handler"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/middleware"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/base-common/app"
	"gitee.com/liuxuezhan/ar-platform/base-common/app/serverMiddleware"
	"gitee.com/liuxuezhan/ar-platform/base-common/xcode"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
	md "github.com/zeromicro/go-zero/rest/handler"
	"github.com/zeromicro/go-zero/rest/httpx"
)

var configFile = flag.String("f", "assets-api/etc/lxz.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	// 初始化后内置调整
	app.InitAppServer()

	ctx := svc.NewServiceContext(c)
	var opt = []serverMiddleware.SMOption{
		serverMiddleware.WithWhiteHeaderPathSMOption(middleware.WhiteHeaderPath()),
		serverMiddleware.WithPlatformBusinessFuncSMOption(middleware.GetPlatformBusinessFunc(c)),
		serverMiddleware.WithCheckTokenHandleSMOption(middleware.CheckTokenHandle(c)),
	}

	if c.IsDebug {
		opt = append(opt, serverMiddleware.WithDebugOption())
	}
	serverMiddleware.NewServerMiddleware(server, opt...).ApiUseMiddleware()
	server.Use(rest.ToMiddleware(md.DetailedLogHandler))

	handler.RegisterHandlers(server, ctx)
	// 自定义错误处理方法
	httpx.SetErrorHandler(xcode.ErrHandler)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
	// HttpsServer()
}
func HttpsServer() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	// 初始化后内置调整
	app.InitAppServer()

	ctx := svc.NewServiceContext(c)
	var opt = []serverMiddleware.SMOption{
		serverMiddleware.WithWhiteHeaderPathSMOption(middleware.WhiteHeaderPath()),
		serverMiddleware.WithPlatformBusinessFuncSMOption(middleware.GetPlatformBusinessFunc(c)),
		serverMiddleware.WithCheckTokenHandleSMOption(middleware.CheckTokenHandle(c)),
	}

	if c.IsDebug {
		opt = append(opt, serverMiddleware.WithDebugOption())
	}
	serverMiddleware.NewServerMiddleware(server, opt...).ApiUseMiddleware()
	server.Use(rest.ToMiddleware(md.DetailedLogHandler))

	handler.RegisterHandlers(server, ctx)
	// 自定义错误处理方法
	httpx.SetErrorHandler(xcode.ErrHandler)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
